package com.springboot.learn_zookeeper.demo1;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;

public class Master implements Watcher {

    ZooKeeper zk;
    String hostPort;

    //一个对象的构造函数没有完成前不要调用这个对象的其他方法
    Master(String hostPort){
        this.hostPort=hostPort;
    }

    //使用Master对象来构造Zookeeper对象，以便添加Watcher的回调函数
    void startZk() throws IOException {
        zk=new ZooKeeper(hostPort,15000,this);
    }

    @Override
    public void process(WatchedEvent event) {
        System.out.println(event);
    }

    public static void main(String[] args) throws InterruptedException, IOException {
        Master master=new Master(args[0]);
        master.startZk();

        //wait for a bit
        Thread.sleep(60000);
    }
}
